package management_system;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        try {
            StudentManager manager = new StudentManager();

            Student s1 = new Student("1001", "张三");
            Student s2 = new Student("1002", "李四");
            Student s3 = new Student("1003", "王五");
            manager.addStudent(s1);
            manager.addStudent(s2);
            manager.addStudent(s3);

            /* 你的数据 */
            manager.addScoreToStudent("1001", "math", 95);
            manager.addScoreToStudent("1001", "java", 88);
            manager.addScoreToStudent("1001", "english", 76);
            manager.addScoreToStudent("1002", "math", 62);
            manager.addScoreToStudent("1002", "java", 72);
            manager.addScoreToStudent("1002", "english", 90);
            manager.addScoreToStudent("1003", "???", -80); // 异常，写入 null

            String csvPath = "E:\\desktop\\students.csv";
            String txtPath = "E:\\desktop\\class_avg.txt";

            /* 1. 写 CSV */
            FileUtil.saveToCsv(manager.getStudents(), csvPath);
            System.out.println("✅ CSV 已保存到 " + csvPath);

            var report   = FileUtil.readFromCsv(csvPath);
            var students = (List<Student>) report.get("students");
            double avg   = (Double) report.get("classAvg");
            int validCnt = (Integer) report.get("validCount");

            FileUtil.writeTxt(txtPath, students, avg, validCnt);

            /* 4. 写 TXT（全班无成绩时写 "null"） */
            System.out.println("✅ 班级均分已写入 " + txtPath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}